/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).ready(function(){
    $('.addToCart').click(function() {
        var id = $(this).attr('id');
        var price = $('#price' + id).text();
        var quantity = $('#quantity' + id + " option:selected").val();
        
        $.ajax({
            url: '/ajax/order',
            type: 'POST',
            data: {
                'id'        : id,
                'quantity'  : quantity
            },
            success: function(result){
                var json = $.parseJSON(result);
                if(json.status == true){
                    $('#cart').fadeOut('fast').load('/cart/index').fadeIn("fast");
                }
            },
            error: function() {
                alert("error");
            }
        });
        return false;
    });
    $('#clear_cart').live("click", function() {
        $.ajax({
            url: '/ajax/clearcart',
            type: 'POST',
            success: function(result) {
                var json = $.parseJSON(result);
                if (json.status == true) {
                    $('#cart').fadeOut('fast').load('/cart/index').fadeIn("fast");
                }
            },
            error: function() {
                alert("error");
            }
        });
        return false;
    });
    $('#clear_pcart').live("click", function() {
        $.ajax({
            url: '/ajax/clearcart',
            type: 'POST',
            success: function(result) {
                var json = $.parseJSON(result);
                if (json.status == true) {
                    $('#p-cart').fadeOut('fast').load('/cart/precheckout').fadeIn("fast");
                }
            },
            error: function() {
                alert("error");
            }
        });
        return false;
    });
   
    $('.remove').live("click", function() {
        var rid = $(this).attr('rid');
        $.ajax({
            url: '/ajax/removeproduct',
            type: 'POST',
            data: {
                'rid'        : rid
            },
            success: function(result) {
                var json = $.parseJSON(result);
                if (json.status == true) {
                    $('#cart').fadeOut('fast').load('/cart/index').fadeIn("fast");
                }
            },
            error: function() {
                alert("error");
            }
        });
        return false;
    });

    /*$(".t1").live('mouseenter', function(){
        var $this = $(this);
        $this.stop(true, true);
        $this.animate({
            "top": 500 + 'px',
            'left': 700 + 'px'
        }, 400, function(){
            $this.removeClass('t1').addClass('t2');
            $this.animate({
                "top": 500 + 'px',
                'left': 600 + 'px'
            }, 4000, function(){
                $this.animate({
                    "top": 600 + 'px',
                    'left': 550 + 'px'
                }, 4000, function(){
                    $this.removeClass('t2').addClass('t3');
                    $this.animate({
                        "top": 600 + 'px',
                        'left': 550 + 'px'
                    }, 400, function(){
                        $this.removeClass('t3').addClass('t4');
                        $this.animate({
                            "top": 600 + 'px',
                            'left': 550 + 'px'
                        }, 400, function(){
                            $this.removeClass('t4').addClass('t3');
                            $this.animate({
                                "top": 600 + 'px',
                                'left': 550 + 'px'
                            }, 400, function(){
                                $this.removeClass('t3').addClass('t2');
                                $this.animate({
                                    "top": -107 + 'px',
                                    'left': -150 + 'px'
                                }, 1500, function(){
                                    $this.removeClass('t2').addClass('t1');
                                    $this.animate({
                                        "top": 20 + 'px',
                                        'left': 70 + 'px'
                                    }, 15000);
                                });
                            });
                        });
                    });
                });
            });
        });
    });

    $(".t0").live('click', function(){
        var $this = $(".t0");
        $this.stop(true, true).animate({
            "opacity": "0"
        }, 500, function(){
            $this.remove();
        });
        trollTrue = false;
    });*/
    
    $('.remove_from_order').live('click', function(){
        var pid = $(this).attr('pid');
        
        
        var total_for_current_order = $('#tfco' + pid).text();
        total_for_current_order = parseInt(total_for_current_order);


        $.ajax({
            url: '/ajax/removefromorders',
            type: 'POST',
            data: {
                'pid'        : pid
            },
            success: function(result) {
                var json = $.parseJSON(result);
                if (json.status == true) {
                    $('#tr' + pid).remove();
                    var total_sum = $('#total_sum').text();
                    total_sum = parseInt(total_sum);
                    var sum_for_all = total_sum - total_for_current_order;
                    $('#total_sum').text(sum_for_all);
                    
                }
            },
            error: function() {
                alert("error");
            }
        });
        return false;
    });
    
    $('#merge_garnirs').live({
        click: function() {
            var uid = $(this).attr('uid');
            $('#pop_up_div').show();
            $('#pop_up_div').draggable();
            $('#close_button').live({
                click: function() {
                    $('#pop_up_div').hide();
                }
            });
            $.ajax({
                url: '/ajax/prepareOrdersToMerge',
                type: 'POST',
                data: {
                    'uid'        : uid
                },
                success: function(result) {
                    var json = $.parseJSON(result);
                    if (json.status == true) {
                        $('#products_to_merge').html("");
                        $('#products_to_merge').append(json.garnirs);
//                        console.log(json.garnirs);
                    }
                }
            });
            return false;
        }
    });
    
    
    
    $('#finish_merging').live({
        click: function() {
            var food = new Array();
            
            if ($('.merging_tr_second_dishes')) {
                
                var merge_all = $('.merging_tr_second_dishes').each(
                    function () {
                        var second_dish = $(this).find(".second_dishes").attr("id");
                        var garnir = $(this).find(".garnir_select option:selected").val();
                        var temp_array = new Array();
                        
                        temp_array.push(second_dish);
                        temp_array.push(garnir);
                        food.push(temp_array);
                        }
                    );
            }
            
            
            if ($('.merging_tr_garnirs')) {
                var merge_all = $('.merging_tr_garnirs').each(
                function () {
                    var garnir = $(this).find(".garnirs").attr("id");
                    var second_dish = $(this).find(".second_dish_select option:selected").val();
                    var temp_array = new Array();
                    temp_array.push(second_dish);
                    temp_array.push(garnir);
                    food.push(temp_array);
                    }
                );
            }
            
            $.ajax({
                url: '/ajax/mergeGarnirs',
                type: 'POST',
                data: {
                    'food'        : food
                },
                success: function(result) {
                    var json = $.parseJSON(result);
                    if (json.status == true) {
                        window.location = "/viewproducts";
                    }                    
                }
            });
            return false;
        }
    });
   
   $('.delete_from_orders').live({
       click: function() {
           var oid = $(this).attr("oid");
           var tbl = $(this).attr("tbl");
            
            $.ajax({
                url: '/ajax/deleteOrder',
                type: 'POST',
                data: {
                    'oid'        : oid,
                    'tbl'        : tbl
                },
                success: function(result) {
                    var json = $.parseJSON(result);
                    if (json.status == true) {
                        location.reload();
                    }                    
                }
            });
            return false;
        }
    });
    /*var va = function(){

        var div = document.createElement('div');

        div.setAttribute('id', 'tarakan');
        div.setAttribute('class', 't1');
        $('body').append(div);
    
        $("#tarakan").animate({
            "top": 20 + 'px',
            'left': 70 + 'px'
        }, 4000);

        }
        setTimeout(va, 60000);
        setTimeout(troll, 120000);*/
    });

var trollTrue = true;
var trollVal = 0;

    /*var troll = function troll(){
        var div = document.createElement('div');
        div.setAttribute('class', 'tarakan t0');
        jQuery('body').append(div);
        var toper = Math.random()*800;
        var lefter = Math.random()*1500;
        $(div).animate({
            "top": toper + 'px',
            'left': lefter + 'px'
        }, lefter);
        if(trollTrue && trollVal < 200){
            setTimeout(troll, lefter);
            trollVal++;
        }
    }*/